<?php
// 财务收支
namespace app\home\controller;
use think\Db;
class Proof extends HomeBase {

	public function getlist() {
		$map = [];
		$townsid = input('townsid','0','intval');
		$order = input('order');				// 排序
		$page = input('page','1','intval');		// 当前页
		$row = input('row','10','intval');		// 每页显示数据

		// 查询条件
		if ($townsid) $map['townsid'] = $townsid;

		// 排序
		if (empty($order)) $order = 'create_time desc';

		// 分页
		$start = ($page-1) * $row;

		if (!empty($map)) {
			$total = Db::name('proof')->where($map)->group('year, month, townsid')->count('id');
			$list = Db::name('proof')->where($map)->order($order)->limit($start,$row)->field('year, month, townsid')->select();
		} else {
			$total = Db::name('proof')->group('year, month, townsid')->count('id');
			$list = Db::name('proof')->order($order)->limit($start,$row)->field('year, month, townsid')->select();
		}

		if (empty($list)) return json(array('status'=>0,'msg'=>'暂无数据'));

		foreach ($list as &$value) {
			$towns = Db::name('towns')->where('code',$value['townsid'])->field('tname')->find();
			$value['nianyue'] = $value['year'].'年'.$value['month'].'月';
			$value['timestamp'] = strtotime($value['year'].'/'.$value['month'].'/01');
			if ($towns && isset($towns['tname'])) {
				$value['towns'] = $towns['tname'];
			} else $value['towns'] = '';
		}
		return json(array('status'=>1,'info'=>$list,'count'=>$total));
	}

	public function getmtlist() {
		$map = array();
		$order = input('order');				// 排序
		$page = input('page','1','intval');		// 当前页
		$row = input('row','10','intval');		// 每页显示数据

		$timestamp = input('timestamp','','intval');
		$townsid = input('townsid','','intval');

		if (empty($timestamp) || empty($townsid)) return json(array('status'=>0,'msg'=>'参数丢失'));

		// 排序
		if (empty($order)) $order = 'create_time desc';
		
		// 分页
		$start = ($page-1) * $row;

        $e_time = mktime(0,0,0,date('m',$timestamp)+1,1,date('Y',$timestamp));
        $map['townsid'] = $townsid;

		$total = Db::name('proof')->where($map)->where('create_time','>=',$timestamp)->where('create_time','<',$e_time)->count('id');
		$list = Db::name('proof')->where($map)->where('create_time','>=',$timestamp)->where('create_time','<',$e_time)->order($order)->limit($start,$row)->select();

		if (empty($list))
			return json(array('status'=>0,'msg'=>'暂无数据'));
		else {
			return json(array('status'=>1,'info'=>$list,'count'=>$total));
		}
	}
}